<template>
  <CommonDialog v-model="visible" title="切片详情" :width="600" submit-text="确定"
    @submit="submit" @cancel="cancel">
    <Form ref="formEl" :model="formData" :rules="rules" :label-width="100">
      <FormItem label="切片内容" prop="content">
        <Input v-model="formData.content" type="textarea" placeholder="请输入切片内容" :rows="16" />
      </FormItem>
    </Form>
  </CommonDialog>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { Message } from 'view-ui-plus'

import { myKnowledge } from '@/api/modules/knowledgeBase'

const emit = defineEmits(['cancel', 'submit'])

const props = defineProps({
  currentTypeData: {
    type: Object,
    default: () => {
      return {}
    }
  }
})

// 是否打开弹窗
const visible = ref(true)

// 加载状态
const loading = ref(false)

const dataId = ref(undefined)
// 表单元素ref
const formEl = ref(null)
// 表单数据
const formData = ref({
  content: ''
})

// 表单校验条件
const rules = ref({
  content: [{ required: true, message: '切片内容不能为空', trigger: 'blur' }]
})

/**
 * 提交表单
 */
const submit = async () => {
  let valid = await formEl.value.validate()
  if (!valid) return

  loading.value = true

  try {
    const params = {
      ...formData.value,
      id: dataId.value
    }

    await myKnowledge.sliceEdit({
      data: params
    })

    Message.success('操作成功')
    emit('submit')
  } catch (error) {
    console.error(error)
  }

  loading.value = false
}

/**
 * 取消表单
 */
const cancel = () => {
  emit('cancel')
}

const initData = (data = {}) => {
  dataId.value = data.id
  formData.value.content = data.content || ''
}

defineExpose({
  initData
})
</script>

<style scoped lang="scss">
:deep(.ivu-input){
  resize: none;
}
::deep(.ivu-form-item-label) {
  text-align: right;
}
</style>